package cn.xiaoxige.gradleplugin

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)



        launch(Dispatchers.Main) {
            Log.e("xiaoxige", Thread.currentThread().name)
            val userName = async(Dispatchers.IO) {
                getUserName()
            }
            Log.e("xiaoxige", "userName 运行结束. ${Thread.currentThread().name}")
            val userAge = async(Dispatchers.IO) {
                getUserAge()
            }
            Log.e("xiaoxige", "userAge 运行结束. ${Thread.currentThread().name}")

            Log.e("xiaoxige", "userName: ${userName.await()}, userAge: ${userAge.await()}")


            val userPhont = getUserPhont()
            Log.e("xiaoxige", "userPhoto: $userPhont")
            val userSex = getUserSex()
            Log.e("xiaoxige", "userSex: $userSex")
        }

        Log.e("xiaoxige", "onCreate 结束")
    }

    private fun getUserName(): String {
        Thread.sleep(3000)

        return "xiaoxige"
    }

    private fun getUserAge(): Int {
        return 0
    }

    private suspend fun getUserSex(): String {
        return withContext(Dispatchers.IO) {
            Thread.sleep(3000)
            "男"
        }
    }

    private suspend fun getUserPhont(): String {

        return withContext(Dispatchers.IO) {
            "110"
        }
    }

    override fun onDestroy() {
        cancel()
        super.onDestroy()
    }
}
